package com.longdaji.decoration.ui.activitiesOfMine.pendingPayment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.R;
import com.longdaji.decoration.adapter.PendingPaymentOrderAdapter;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.ui.activitiesOfMine.pendingPayment.payment.PaymentActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PendingPaymentActivity extends BaseActivity implements PendingPaymentContract.View {

    @Inject
    PendingPaymentContract.Presenter mPresenter;
    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.rv_pending_payment)
    RecyclerView rvPendingPayment;
    @BindView(R.id.sr_pending_payment)
    SwipeRefreshLayout srPendingPayment;
    private PendingPaymentOrderAdapter mAdapter;

    private List<Orders.OrdersBean.OrderBean> mOrderList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pending_payment);
        setUnBinder(ButterKnife.bind(this));
        if (mPresenter != null)
            mPresenter.setView(this);
        tvTitle.setText("待支付");

        initView();


    }

    private void initView() {
        rvPendingPayment.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new PendingPaymentOrderAdapter(mOrderList);
        mAdapter.setCancelOrderListener(new PendingPaymentOrderAdapter.CancelOrderListener() {
            @Override
            public void onCancelOrderClick(int position) {
                mPresenter.onCancelOrderClick(Account.getInstance().getUserid(), mOrderList.get(position).getOrderNo());
            }
        });
        mAdapter.setPayListener(new PendingPaymentOrderAdapter.PayListener() {
            @Override
            public void onPayListenerClick(int position) {
                Intent intent = new Intent(PendingPaymentActivity.this, PaymentActivity.class);
                intent.putExtra(Constants.ORDER_NO, mOrderList.get(position).getOrderNo());
                startActivity(intent);
            }
        });

        rvPendingPayment.setAdapter(mAdapter);
        srPendingPayment.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        srPendingPayment.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }
        });
        mPresenter.initData();
    }

    private void refreshData() {
        mPresenter.initData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    @OnClick({R.id.rl_tool_back})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void showPendingPaymentOrders(List<Orders.OrdersBean.OrderBean> pendingPaymentOrderList) {
        Log.d("order test", "size : " + pendingPaymentOrderList.size());
        Collections.reverse(pendingPaymentOrderList);
        if (pendingPaymentOrderList.size() > 0) {
            mAdapter.setData(pendingPaymentOrderList);
        } else {
            Toast.makeText(this, "无待付款订单", Toast.LENGTH_SHORT).show();
            mAdapter.clearData();
        }
        if (srPendingPayment != null && srPendingPayment.isRefreshing()) {
            srPendingPayment.setRefreshing(false);
        }
    }

    @Override
    public void showCancelOrderSucceed() {
        Toast.makeText(this, "成功取消订单", Toast.LENGTH_SHORT).show();
    }
}
